﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Security.Cryptography.X509Certificates;
using System.Configuration;

namespace 项目
{
    class Program
    {
        static void Main(string[] args)
        {
            SqlDataReader dr = null;
            int button;
            Console.WriteLine("【1】查询【2】增删改【3】退出");
            button = int.Parse(Console.ReadLine());
            //查询操作
            if (button==1)
            {
                try
                {
                    string sql = "select *from Goods";
                    //执行SQL命令，返回DataReader对象
                    dr = DBHelper.GetDataReader(sql);
                    Console.WriteLine("编号/t商品名/t单价/t库存/t原价/t状态");
                    Console.WriteLine("------------------------------------");
                    //使用循环，逐行读取数据
                    while (dr.Read())
                    {
                        String id = dr["GoodsID"].ToString();
                        String name = dr["GoodsName"].ToString();
                        String UnitPrice = dr["UnitPrice"].ToString();
                        String Lnventory = dr["Lnventory"].ToString();
                        String Did = dr["Did"].ToString();
                        String Returnn = dr["Returnn"].ToString();
                        Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}\t{5}", id, name, UnitPrice, Lnventory, Did, Returnn);
                    }
                }
                catch (Exception ex)
                {

                    //检测到异常进行异常处理，防止应用程序崩溃，打印信息提示用户
                    Console.WriteLine("加载商品列表时出现异常，请联系管理员！ 原因：" + ex.Message);
                }
                finally
                {
                    //无论是否发生异常，都会执行finally块
                    //关闭连接
                    if (dr != null)
                        //关闭DataReader连接对象
                        dr.Close();
                }
            

            }
            //增删改操作
            if (button == 2)
            {
                int num;
                Console.WriteLine("请输入想要的操作：【1】增加商品【2】修改商品【3】删除商品");
                num = int.Parse(Console.ReadLine());
                try
                {
                   
                    //增加商品
                    if (num == 1)
                    {

                        Console.Write("请输入商品编号：");
                        string GoodsID;
                        GoodsID = Console.ReadLine();
                        Console.Write("请输入商品名字：");
                        string GoodsName;
                        GoodsName = Console.ReadLine();
                        Console.Write("请输入商品单价：");
                        string UnitPrice;
                        UnitPrice = Console.ReadLine();
                        Console.Write("请输入商品库存：");
                        string Lnventory;
                        Lnventory = Console.ReadLine();
                        Console.Write("请输入商品原价：");
                        string Did;
                        Did = Console.ReadLine();
                        Console.Write("请输入商品状态：");
                        string State;
                        State = Console.ReadLine();
                        //创建插入的Sql语句
                     string   sqlInt = string.Format("insert into Goods values('{0}','{1}','{2}','{3}','{4}','{5}')", GoodsID, GoodsName, UnitPrice, Lnventory, Did, State);
                        bool result = DBHelper.ExcuteNonQuery(sqlInt);
                        if (result)
                        {
                            Console.WriteLine("添加商品成功！！");
                        }
                        else
                        {
                            Console.WriteLine("添加商品失败！！！");
                        }

                    }
                   //修改
                    if (num == 2)
                    {
                        Console.Write("请输入修改的商品编号：");
                        string GoodsID = Console.ReadLine();
                        Console.Write("请输入修改的商品名字：");
                        string GoodsName = Console.ReadLine();
                        Console.Write("请输入修改的商品单价：");
                        string UnitPrice = Console.ReadLine();
                        Console.Write("请输入修改的商品库存：");
                        string Lnventory = Console.ReadLine();
                        Console.Write("请输入修改的商品原价：");
                        string Did = Console.ReadLine();
                        Console.Write("请输入修改的商品状态：");
                        string State = Console.ReadLine();
                        //创建插入的Sql语句
                       string sqlUp = string.Format("update Goods values('{0}','{1}','{2}','{3}','{4}','{5}')", GoodsID, GoodsName, UnitPrice, Lnventory, Did, State);
                        bool result = DBHelper.ExcuteNonQuery(sqlUp);
                        if (result)
                        {
                            Console.WriteLine("修改商品信息成功！！");
                        }
                        else
                        {
                            Console.WriteLine("修改商品信息失败！！！");
                        }


                    }
                    //删除
                    if (num == 3)
                    {
                        Console.Write("请输入删除的商品编号：");
                        string GoodsID = Console.ReadLine();
                        string sqlD = string.Format("delete from Goods where GoodsID='GoodsID'") ;
                        bool result = DBHelper.ExcuteNonQuery(sqlD);
                        if (result)
                        {
                            Console.WriteLine("修改商品信息成功！！");
                        }
                        else
                        {
                            Console.WriteLine("修改商品信息失败！！！");
                        }
                    }
                   


                }
                catch (Exception ex)
                {

                    //检测到异常进行异常处理，防止应用程序崩溃，打印信息提示用户
                    Console.WriteLine("加载商品列表时出现异常，请联系管理员！ 原因：" + ex.Message);
                }
                finally
                {
                    //无论是否发生异常，都会执行finally块
                    //关闭连接
                    if (dr != null)
                        //关闭DataReader连接对象
                        dr.Close();
                }
            }
        }
    }
}
